package com.gxa.community.customer.controller.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.gxa.community.core.pojo.domain.OrderInfo;
import com.gxa.community.core.utils.R;
import com.gxa.community.customer.service.pay.PayService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;


@Slf4j
@RestController
@RequestMapping("/pay")
@ApiModel("支付接口")
public class PayController {

    @Autowired
    private PayService payService;

    @ApiOperation("订单支付接口")
    @PostMapping("/orderpay")
    public String orderPay(@RequestBody OrderInfo orderInfo){
        String result = payService.orderPay(orderInfo);
        return null;
    }


    @RequestMapping("/payReturn")
    public String payReturn(HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException, JsonProcessingException {
        /**
         * 在该接口使用消息队列，支付成功  则取修改后台相关数据
          */
        return payService.payReturn(request);
    }


    @ApiOperation("退款接口")
    @PostMapping("/refund")
    public R refund(@RequestParam("orderNo")String orderNo,@RequestParam("payment")Double payment) throws AlipayApiException {
        return payService.refund(orderNo,payment);
    }

}
